<template>
    <div class="new-arrivals">
      <h2 class="section-title">新品上市</h2>
      <div class="product-list">
        <div 
          v-for="product in products" 
          :key="product.id" 
          class="product-card"
        >
          <div class="new-tag">NEW</div>
          <img :src="product.productMainImg" :alt="product.productMainImg" class="product-image" />
          <div class="product-info">
            <h3 class="product-name">{{ product.productName }}</h3>
            <div class="price-section">
              <span class="price">¥{{ product.productPrice }}</span>
            </div>
            <button class="detail-btn" @click="goToProductDetail(product.id)">查看详情</button>
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref,onMounted } from 'vue'
  import { useRouter } from 'vue-router'
  import { productAPI } from '@/http'
  
  const router = useRouter()
  
  const products = ref([
    {
      "id": 1027,
      "productName": "",
      "productBrand": "",
      "productType": "",
      "productPrice": 0,
      "productColor": "",
      "productSize": "",
      "productMainImg": "",
      "productCount": 0,
      "productPoint": 0,
      "productArea": "",
      "productSold": 0,
      "xx": "0",
      "yy": null,
      "productVersion": null,
      "productStatus": null
    }
  ])

  // 接收后端数据的函数
  const fetcheProduct = async () => {
    const response = await productAPI.getRandomProduct(4);
    products.value = response.data;
    console.log(products.value);
  }

  // 页面刷新时重新获取数据
  onMounted(() =>
    fetcheProduct()
  )
  
  const goToProductDetail = (id) => {
    router.push(`/product/${id}`)
  }
  </script>
  
  <style scoped>
  .new-arrivals {
    margin-bottom: 40px;
  }
  
  .product-list {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20px;
  }
  
  .product-card {
    background: #fff;
    border-radius: 8px;
    overflow: hidden;
    position: relative;
    transition: all 0.3s;
  }
  
  .product-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  }
  
  .new-tag {
    position: absolute;
    top: 10px;
    right: 10px;
    background: #ff6700;
    color: #fff;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 12px;
    z-index: 1;
  }
  
  .product-image {
    width: 100%;
    height: 200px;
    object-fit: cover;
  }
  
  .product-info {
    padding: 15px;
  }
  
  .product-name {
    font-size: 16px;
    margin: 0px;
    height: 40px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  
  .price-section {
    margin-bottom: 15px;
  }
  
  .price {
    color: #ff6700;
    font-size: 20px;
    font-weight: bold;
  }
  
  .detail-btn {
    width: 100%;
    padding: 8px 0;
    background: #fff;
    color: #ff6700;
    border: 1px solid #ff6700;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.3s;
  }
  
  .detail-btn:hover {
    background: #ff6700;
    color: #fff;
  }
  </style>